package com.csy.commons.core.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.csy.commons.core.exception.BaseException;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**异常工具类
 */
public class ExcelUtils {

    private static final Logger log = LoggerFactory.getLogger(ExcelUtils.class);
    /*
    * excel导出
    * list数据列表
    * fileName导出的Excel名称
    * exportParams导出参数（标题，sheet名称，是否创建表头，表格类型）
    * */
    public static void exportExcel(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams){

        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        downLoadExcel(fileName,response,workbook);
    }
    /*
    * Excel下载
    * fileName文件名
    * workbookExcel数据对象
    * */
    private static void downLoadExcel(String fileName,HttpServletResponse response,Workbook workbook){
        ServletOutputStream outputStream = null;
        try {
            response.setCharacterEncoding("UTF-8");
            //设置文件名和下载方式
            response.setHeader("content-disposition","attachment;fileName=" + URLEncoder.encode(fileName,"UTF-8"));
            outputStream = response.getOutputStream();
            workbook.write(outputStream);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出Excel异常：{}",e.getMessage());
            throw new BaseException("500","导出Excel失败,请联系网站管理员!");
        }finally {
            try {
                outputStream.close();
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


}

